package com.chuanyang.driverh.mainmy;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.bean.IdentifyCarBean;
import com.chuanyang.driverh.mainmy.fragment.IdentifyCarFragment;
import com.chuanyang.driverh.mainmy.fragment.IdentifyCarPhotoFragment;
import com.chuanyang.driverh.responseresult.UpLoadImageResult;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.camera.CameraUtils;
import com.chuanyang.driverh.utils.eventbus.EventBusUtil;
import com.chuanyang.driverh.utils.eventbus.ImageEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;
import com.chuanyang.driverh.utils.widget.MyViewPager;

import java.io.File;
import java.util.ArrayList;

import okhttp3.Call;

/**
 *
 */

public class CarIdentifyActivity extends BaseTitleActivity {
    private MyViewPager pager;
    private ArrayList list;
    private IdentifyCarBean identifyCarBean;

    private int currentIndex = -1;
    public void setCurrentIndex(int index){
        this.currentIndex=index;
    }
    @Override
    protected int getContentResId() {
        return R.layout.my_act_identify;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        pager = (MyViewPager) findViewById(R.id.vp_identify);
    }

    @Override
    protected void initData() {
        setBackImg();
        getTitleTextView().setText("认证");
        identifyCarBean = new IdentifyCarBean();
        list = new ArrayList();
        Fragment identifyCarFragment = new IdentifyCarFragment();
        Fragment identifyCarPhotoFragment = new IdentifyCarPhotoFragment();
        list.add(identifyCarFragment);
        list.add(identifyCarPhotoFragment);

    }


    @Override
    protected void initListener() {
        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return (Fragment) list.get(position);
            }
            @Override
            public int getCount() {
                return list.size();
            }
        });

    }

    public IdentifyCarBean getIdentifyCarBean() {
        return identifyCarBean;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_titlebar_back:
                if (pager.getCurrentItem() == 0) {
                    finish();
                } else if (pager.getCurrentItem() == 1) {
                    pager.setCurrentItem(0);
                }
                break;
        }
    }


    public MyViewPager getPager() {
        return pager;
    }



    private void uploadCarImage(final File file) {
        DialogUtil.showLoadingDialog(activityContext,"上传图片...");
        MyHttpUtil.uploadDriverImg(activityContext,file, new MyCallback<UpLoadImageResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(UpLoadImageResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    EventBusUtil.postEvent(new ImageEvent(result.getData(), currentIndex));
                    file.delete();
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });


    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == CameraUtils.REQUEST_CODE_TAKE_PICTURE) {//直接拍照
                //                photopath = CameraUtils.mCurrentPhotoPath;
                File photo = BitmapUtil.compressImage(CameraUtils.mCurrentPhotoPath, this);
                uploadCarImage(photo);

            }
            if (requestCode == CameraUtils.REQUEST_CODE_SELECT_PICTURE && data != null) {//从相册中选择
                if (data.getData() != null) {
                    //                    photopath = CameraUtils.getPath(this, data.getData());
                    File photo = BitmapUtil.compressImage(CameraUtils.getPath(this, data.getData()), this);
                    uploadCarImage(photo);
                }
            }
        }
    }
}
